package zt.song.shopping.controller;

import cn.hutool.core.util.IdUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.javassist.bytecode.Mnemonic;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zt.song.shopping.domain.*;
import zt.song.shopping.service.GoodsService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/12/6
 * @Version 1.0
 **/
@Controller
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @GetMapping("/list")
    public String list(Model model){
        model.addAttribute("list",this.goodsService.list());
        return "list";
    }

    @GetMapping("/buy")
    public  String buy(int gid,HttpSession session){
        //根据id查询要购买的商品？
        Goods goods = this.goodsService.getById(gid);
        //从session获取购物车对象
        List<ShoppingCart> list = (List<ShoppingCart>) session.getAttribute("shoppingCart");
        if(null == list){
            list = new ArrayList<>();
            ShoppingCart cart= new ShoppingCart();
            cart.setGoods(goods);
            cart.setCount(1);
            goods.setAmount(goods.getAmount()-1);
            list.add(cart);
            session.setAttribute("shoppingCart",list);
        }else{
            ShoppingCart temp =null;
            for(ShoppingCart cart: list){
                if(cart.getGoods().getGid() == gid){
                    temp = cart;
                    break;
                }
            }
            if(temp!=null){
                //goods.setAmount(goods.getAmount()-1);
                temp.setCount(temp.getCount()+1);

            }else{
                ShoppingCart cart= new ShoppingCart();
                cart.setGoods(goods);
                cart.setCount(1);
                list.add(cart);
            }
        }
        int total =0;
        for(ShoppingCart cart: list){
            BigDecimal price = cart.getGoods().getPrice();
            Integer count = cart.getCount();
            total += price.multiply(new BigDecimal(count)).intValue();
        }
        session.setAttribute("total",total);
//            new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
        session.setAttribute("shoppingCart",list);
        return  "redirect:/list";
    }

     @GetMapping("/showShopping")
     public String show(){
        //从session中获取数据
         return "shoppingCart";
     }

     @GetMapping("/pay")
     public String pay(HttpSession session){
        //获取用户对象
         User user = (User)session.getAttribute("user");
         int userid=user.getUid();
         Integer total = (Integer) session.getAttribute("total");

         List<ShoppingCart> shoppingCart = (List<ShoppingCart>)session.getAttribute("shoppingCart");
         if(this.goodsService.pay(userid,total,shoppingCart)){
             //清空购物车
             shoppingCart.clear();
             session.setAttribute("total",0);
             return "redirect:/list";
         }
         return "";
     }

     @GetMapping("/delete")
     public @ResponseBody boolean delete(String gids,HttpSession session){
         List<ShoppingCart> shoppingCart = (List<ShoppingCart>)session.getAttribute("shoppingCart");
         //删除集合指定的对象
         String[] arr= gids.split(",");
         for(Iterator<ShoppingCart> iterator = shoppingCart.iterator();iterator.hasNext();){
                ShoppingCart cart= iterator.next();
               Goods goods = cart.getGoods();
               for(String s: arr) {
                   if(Integer.parseInt(s)== goods.getGid()){
                       iterator.remove();
                   }
               }
         }
         int total =0;
         for(ShoppingCart cart: shoppingCart){
             BigDecimal price = cart.getGoods().getPrice();
             Integer count = cart.getCount();
             total += price.multiply(new BigDecimal(count)).intValue();
         }
         session.setAttribute("total",total);
        //session.setAttribute("shoppingCart",shoppingCart);
        return  true;
     }


     @GetMapping("/showHistory")
    public String showHistory(HttpSession session,Model model) {
         User user = (User) session.getAttribute("user");
         int userid = user.getUid();
         model.addAttribute("list",this.goodsService.showHistory(userid));
         return "history";

     }
}
